package com.yuan.homework.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

/**
 * @program: Train
 * @description:
 * @author: yuan_boss
 * @create: 2022-11-21 19:30
 **/
@WebFilter(urlPatterns = {"/*"},initParams = {@WebInitParam(name = "encoding",value = "utf-8")})
public class EncodingFilter implements Filter {
    private String encode;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encode = filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding(encode);
        response.setContentType("text/html;charset=" + encode);
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
